<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var data = [
            { pid: 0, id: 'a', value: '陕西' },
            { pid: 'a', id: 01, value: '西安' },
            { pid: 01, id: 301, value: '雁塔区' },
            { pid: 01, id: 302, value: '高新区' },
            { pid: 'a', id: 02, value: '渭南' },
            { pid: 'a', id: 03, value: '咸阳' },
            { pid: 0, id: 'b', value: '广东' },
            { pid: 'b', id: 11, value: '广州' },
            { pid: 'b', id: 12, value: '深圳' },
            { pid: 'b', id: 13, value: '潮汕' },
            { pid: 0, id: 'c', value: '湖南' },
            { pid: 'c', id: 21, value: '长沙' },
            { pid: 'c', id: 22, value: '常德' },
            { pid: 'c', id: 23, value: '岳阳' },
        ];
        function toTree(data) {
            data.forEach(item => {
                delete item.children
            });
            var mm=[]
            data.forEach(item=>{
                mm[item.id]=item
            })
            var val=[]
            data.forEach(item=>{
                var parent=mm[item.pid]
                if(parent){
                    (parent.children||(parent.children=[])).push(item)

                }else{
                    val.push(item)
                }
            })
            return val
        }
console.log(toTree(data));

    </script>
</body>

</html>